package com.kancy.drools.learning.globalvar;

import com.kancy.drools.learning.BaseTests;
import org.junit.Test;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;

/**
 * @author kancy
 * @date 2020/1/19 11:21
 **/
public class GlobalVarTests extends BaseTests {
    @Test
    public void test(){
        KieContainer kieContainer = getKieContainer();
        KieSession kieSession = kieContainer.newKieSession("globalvar-session");
        kieSession.setGlobal("globalMap", new HashMap<String, Object>());
        kieSession.setGlobal("globalList", new ArrayList<>());
        kieSession.setGlobal("globalSet", new HashSet<>());
        kieSession.fireAllRules();
        // 打印全局变量
        kieSession.getGlobals().getGlobalKeys().stream().forEach(name ->{
            System.out.println(kieSession.getGlobal(name));
        });
        kieSession.dispose();
    }
}
